package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.ComplaintRecord;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.utils.annotation.RequiredPermissions;
import com.matt.service.ComplaintRecordService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/pad/complaintRecord")
@Slf4j
@Api(tags = "投诉")
public class PadComplaintRecordController {


    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private ComplaintRecordService complaintRecordService;



    /**微信端
     * 新增投诉与建议
     * @param usersId
     * @param childrenId
     * @param complaintObject
     * @param title
     * @param content
     * @param files 图片文件列表。可以传多个图片。多个图片已"|"分隔
     * @return
     */
    @PostMapping("/addRecord_upload")
    public StatusResponse addComplaintRecord(@RequestHeader(TICKET) String ticket,
                                             String usersId,String childrenId,  String complaintObject,String title,
                                             String content,MultipartFile[] files){
        ComplaintRecord record = complaintRecordService.addComplaintRecord(usersId, childrenId, complaintObject, title, content, files);
        return StatusResponse.success(record);
    }

    /**微信端
     * 新增投诉与建议
     * @param parentPhone
     * @param childrenId
     * @param complaintObject
     * @param title
     * @param content
     * @param files 图片文件列表。可以传多个图片。多个图片已"|"分隔
     * @return
     */
    @PostMapping("/addRecord")
    public StatusResponse addComplaintRecord(@RequestHeader(value = TICKET, required = false) String ticket,
                                             String parentPhone,String childrenId,  String complaintObject,String title,
                                             String content, String files){
        ComplaintRecord record = complaintRecordService.addComplaintRecord(parentPhone, childrenId, complaintObject, title, content, files);
        return StatusResponse.success(record);
    }

    /**微信端
     * 查询家长的投诉列表
     * @param usersId
     * @param query
     * @return
     */
    @PostMapping("/getComplaintRecord")
    public StatusResponse getComplaintRecord(@RequestHeader(value = TICKET, required = false) String ticket, String  usersId, PagerQuery query){
        Page<ComplaintRecord> recordPage = complaintRecordService.getComplaintRecord(usersId, query.getPageNum(), query.getPageSize());
        return StatusResponse.success(recordPage);
    }
    /**
     * 删除
     * @param ticket
     * @param complaintRecordId
     * @return
     */
    @DeleteMapping("/deleteComplaintRecord")
    @RequiredPermissions({PermissionConstant.TEACH_COMPLAINT_DELETE})
    public StatusResponse deleteComplaintRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String complaintRecordId) {
        StaffDTO staff = cache.getStaff(ticket);

        if(null == staff)
            throw new BusinessException(ErrorCode.PROMISSION_DELINED, "权限不足,用户无角色");

        String staffName = staff.getRealName();

        ComplaintRecord record =  complaintRecordService.deleteComplaintRecord(staff.getId(), staffName, complaintRecordId);

        return StatusResponse.success(ModelUtil.mapOf("noticeId", record.getId(), "deleteStatus", record.getDeleted()));
    }

    /**APP
     * 查询投诉列表
     */
    @GetMapping("/getAppComplaintRecord")
    public StatusResponse getAppComplaintRecord(@RequestHeader(value = RequestConstant.TICKET, required = false) String ticket,String  roleCode,String queryRoleCode,Boolean isReply, PagerQuery query){
        StaffDTO staff = cache.getStaff(ticket);

        String staffId = staff.getId();

        //FIXME  这里需要修复
//        StaffRoleDTO staffRole = ssoCallService.getStaffRole(staffId);
//
//        String staffRoleCode = staffRole.getRoleCode();
//
//        if(staffRoleCode.equals(RoleCodeConstant.ROLE_CODE_LEADER)){
//            //该用户拥有最高角色是园长
//            roleCode = RoleCodeConstant.ROLE_CODE_LEADER;
//        }

        Page<ComplaintRecord> recordPage = complaintRecordService.getComplaintRecordByRoleCode(staff.getAreaId(), staffId, roleCode, queryRoleCode, isReply, query.getPageNum(), query.getPageSize());


        return StatusResponse.success(recordPage);
    }


    /**
     * 回复投诉
     * @param ticket
     * @param complaintRecordId
     *@param replyContent 回复内容
     * @return
     */
    @PostMapping("/replyComplaintRecord")
    @RequiredPermissions(PermissionConstant.TEACH_COMPLAINT_REPLY)
    public StatusResponse deleteNotice(@RequestHeader(RequestConstant.TICKET) String ticket, String complaintRecordId,String replyContent) {

        StaffDTO staff = cache.getStaff(ticket);

        String staffName = staff.getRealName();

        ComplaintRecord record =  complaintRecordService.replyComplaintRecord(staff.getId(), staffName, complaintRecordId, replyContent);
        return StatusResponse.success(record);
    }

}
